package com.learn.annotation_config.configuration.imports;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 实现ImportBeanDefinitionRegistrar可注册Bean
 */
public class ImportBeanDefinitionRegistrarImpl implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
        // AnnotationMetadata：导入当前类的@Import注解所在类的其他注解原信息
        String[] basePackages = (String[]) importingClassMetadata.getAnnotationAttributes(
                ComponentScan.class.getName()
        ).get("basePackages");
        // com.learn.annotation_config.configuration
        System.out.println(basePackages[0]);
        
        // BeanDefinitionRegistry：已注册Bean信息
        System.out.println(registry.containsBeanDefinition(ImportConfiguration1.class.getName()));
        
        BeanDefinition beanDefinition = new RootBeanDefinition(ImportConfiguration3.class);
        registry.registerBeanDefinition("ImportConfiguration3", beanDefinition);
    }
}
